package com.sh.cloudrender.application.web.controller;


import com.sh.cloudrender.application.dto.access.AccessDTO;
import com.sh.cloudrender.application.service.AccessService;
import com.sh.cloudrender.application.web.params.ApplyAkParam;
import com.sh.cloudrender.application.web.params.DisableAkParam;
import com.sh.cloudrender.application.web.params.QueryAkParam;
import com.sh.cloudrender.application.web.params.ValidateParam;
import com.sh.cloudrender.common.dto.ShXResponse;
import com.sh.cloudrender.common.utils.ShXResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api/v1/access")
public class AccessController {

    @Autowired
    private AccessService accessService;


    @ResponseBody
    @PostMapping("/create")
    public ShXResponse<AccessDTO> create(@Valid @RequestBody ApplyAkParam param) {
        AccessDTO accessDTO = accessService.create(param.getUserId());
        return ShXResponseUtil.build(200, "exec apply access key and secret success", accessDTO);
    }

    @ResponseBody
    @PostMapping("/disable")
    public ShXResponse<Boolean> disable(@Valid @RequestBody DisableAkParam param) {
        boolean success = accessService.disable(param.getUserId(), param.getAccessKey());
        return ShXResponseUtil.build(200, "exec release access key and secret success", success);
    }


    @ResponseBody
    @PostMapping("/find")
    public ShXResponse<AccessDTO> find(@Valid @RequestBody QueryAkParam param) {
        AccessDTO accessDTO = accessService.findBy(param.getUserId(), param.getAccessKey());
        return ShXResponseUtil.build(200, "exec find access key and secret success", accessDTO);
    }


    @ResponseBody
    @PostMapping("/list")
    public ShXResponse<List<AccessDTO>> list(@Valid @RequestBody QueryAkParam param) {
        List<AccessDTO> accessDTO = accessService.listBy(param.getUserId());
        return ShXResponseUtil.build(200, "exec list access key and secret success", accessDTO);
    }

    @ResponseBody
    @PostMapping("/addressValidate")
    public ShXResponse<Boolean> addressValidate(@Valid @RequestBody ValidateParam param) {
        boolean success = accessService.addressValidate(param.getAccessKeySHA(), param.getEngineId());
        return ShXResponseUtil.build(200, "exec access key validate success", success);
    }

}
